package org.example;

import jakarta.annotation.Resource;
import org.example.dto.InventoryRequest;
import org.example.dto.InventoryResponse;
import org.example.dto.ResultDto;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

@RestController
public class InventoryController {        //消费者模块
    @Resource RestTemplate restTemplate;
    @PostMapping("/isInStock")
    @ResponseStatus(HttpStatus.OK)           //查询
    public ResultDto<List<InventoryResponse>> isInStockMap(@RequestBody List<Map<String,Object>> skus) {    //查询调用
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity(skus,headers);
        ParameterizedTypeReference<ResultDto<List<InventoryResponse>>> pt = new ParameterizedTypeReference<>(){};
              return restTemplate.exchange("http://inventory-service/api/inventory/isInStock",
                HttpMethod.POST,
                entity,pt).getBody();
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public void create(@RequestBody InventoryRequest inventoryRequest){
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity(inventoryRequest,headers);
        restTemplate.postForLocation("http://inventory-service/api/inventory",entity);
    }

    @ResponseStatus(HttpStatus.OK)
    @PutMapping
    public void update(@RequestBody InventoryRequest request){             //添加
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity entity = new HttpEntity(request,headers);
        restTemplate.put("http://inventory-service/api/inventory",entity);
    }


//    public void function(){
//        Function f1 = r->"";    //带参，带返回值
//        Supplier s1 = ()->"";   //无参，有返回值
//        Consumer c1 = r->{};    //有参数，无返回值
//        Predicate p1 = r->r!=null;     //有参数，返回布尔值
//
//    }
}
